<!DOCTYPE html>
<html>
<head>
    <title>海报信息表</title>
    <#include "/header.html">
</head>
<body>
<style>
    th {
        text-align: center;
    }
</style>
<div id="rrapp" v-cloak>
    <div v-show="showList">
        <div class="grid-btn">
            <a class="btn btn-primary" @click="add"><i class="fa fa-plus"></i>&nbsp;新增</a>
            <a class="btn btn-primary" @click="update"><i class="fa fa-pencil-square-o"></i>&nbsp;修改</a>
            <a class="btn btn-primary" @click="del"><i class="fa fa-trash-o"></i>&nbsp;删除</a>
        </div>
        <table id="jqGrid"></table>
        <div id="jqGridPager"></div>
    </div>

    <div v-show="!showList" class="panel panel-default">
        <div class="panel-heading">{{title}}</div>
        <form class="form-horizontal">
            <div class="form-group">
                <div class="col-sm-2 control-label">海报分类</div>
                <div class="col-sm-10">
                    <select id="type" class="form-control" v-model="bPosterInfo.type">
                        <option value="-1" disabled selected hidden>请选择海报分类</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-2 control-label">海报原稿</div>
                <div class="col-sm-10">
                    <input type="file" accept="image/png,image/jpg,image/jpeg" class="form-control" value="选择海报原稿"
                           placeholder="海报原稿" onchange="insertImg(this, 'posterCoverUrl')"/>
                    <img :src="bPosterInfo.cover" alt="海报原稿" id="imgCoverUrl" style="width: 150px; max-height: 300px">
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-2 control-label">海报底图</div>
                <div class="col-sm-10">
                    <input type="file" accept="image/png,image/jpg,image/jpeg" class="form-control" value="选择海报底图"
                           placeholder="海报底图" onchange="insertImg(this, 'posterBaseUrl')"/>
                    <img :src="bPosterInfo.base" alt="海报底图" id="imgBaseUrl" style="width: 150px; max-height: 300px">
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-2 control-label">是否推荐</div>
                <div class="col-sm-10">
                    <label class="radio-inline">
                        <input type="radio" name="elite" value="0" v-model="bPosterInfo.elite"/> 否
                    </label>
                    <label class="radio-inline">
                        <input type="radio" name="elite" value="1" v-model="bPosterInfo.elite"/> 是
                    </label>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-2 control-label">是否会员海报</div>
                <div class="col-sm-10">
                    <label class="radio-inline">
                        <input type="radio" name="member" value="0" v-model="bPosterInfo.member"/> 否
                    </label>
                    <label class="radio-inline">
                        <input type="radio" name="member" value="1" v-model="bPosterInfo.member"/> 是
                    </label>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-2 control-label">删除状态</div>
                <div class="col-sm-10">
                    <label class="radio-inline">
                        <input type="radio" name="status" value="0" v-model="bPosterInfo.status"/> 删除
                    </label>
                    <label class="radio-inline">
                        <input type="radio" name="status" value="1" v-model="bPosterInfo.status"/> 正常
                    </label>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-2 control-label">公众号标识</div>
                <div class="col-sm-10">
                    <select id="gzhCode" class="form-control" v-model="bPosterInfo.gzhCode">
                        <option value="-1" selected>全部平台</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-2 control-label">创建时间</div>
                <div class="col-sm-10">
                    <input disabled type="text" class="form-control" v-model="bPosterInfo.createTime"
                           placeholder="创建时间"/>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-2 control-label">修改时间</div>
                <div class="col-sm-10">
                    <input disabled type="text" class="form-control" v-model="bPosterInfo.updateTime"
                           placeholder="修改时间"/>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-2 control-label"></div>
                <input type="button" class="btn btn-primary" @click="saveOrUpdate" value="确定"/>
                <input type="button" class="btn btn-warning" @click="reload" value="返回"/>
            </div>
        </form>
    </div>
</div>

<script src="${request.contextPath}/statics/js/modules/sys/bposterinfo.js?_${.now?long}"></script>
<script src="https://gosspublic.alicdn.com/aliyun-oss-sdk-6.0.0.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
    // 海报分类
    var bPosterDictEntity = {
        number: 1
    };
    $.ajax({
        type: "POST",
        async: false,
        url: baseURL + 'sys/bposterdict/queryAll',
        contentType: "application/json",
        data: JSON.stringify(bPosterDictEntity),
        success: function (r) {
            if (r.code == 0) {
                for (var i = 0; i < r.bPosterDict.length; i++) {
                    $("#type").append(
                        "<option value=" + r.bPosterDict[i].id + ">" + r.bPosterDict[i].name + "</option>");
                }
            }
        }
    });

    // 公众号标识
    $.ajax({
        type: "POST",
        async: false,
        url: baseURL + 'sys/buserinfo/getUserGzhList',
        contentType: "application/json",
        success: function (r) {
            if (r.code == 0) {
                for (var i = 0; i < r.userGzhList.length; i++) {
                    $("#gzhCode").append(
                        "<option value=" + r.userGzhList[i].gzhCode + ">" + r.userGzhList[i].gzhName + "</option>");
                }
            }
        }
    });

    function insertImg(e, pathname) {
        let files = e.files;
        let length = files.length;
        upload(files[0], pathname);
    }

    function upload(file, pathname) {
        console.log(file);
        console.log(pathname);
        let client = new window.OSS({
            region: 'oss-cn-shenzhen', // 你的oss地址，hangzhou，shanghai，shenzhen
            accessKeyId: 'LTAI4Fh7PnYLw3uQvoUYiazS', //你的ak
            accessKeySecret: 'cEw7e8QMz0h4XY4qmzCoTzhHITqyUU', //你的secret
            //stsToken: '<Your securityToken(STS)>',//这里我暂时没用，注释掉
            bucket: 'zktuistatic', //你的oss名字
        });

        // 组装文件名
        let date = new Date();
        let yeaer = date.getFullYear();
        let month = date.getMonth() + 1;
        let day = date.getDate();
        if (month < 10) {
            month = "0" + month;
        }
        if (day < 10) {
            day = "0" + day;
        }
        let datePath = yeaer + "" + month + "" + day;
        let key = "";
        key = pathname + "/" + datePath + "/image" + String(parseInt(Math.random() * 10000000, 10) + getFileExtendingName(
            file.name));

        client.put(key, file).then(function () {
            return client.get(key);
        }).then(function (ret) {
            console.log(ret.res.requestUrls[0]);
            let reqUrl = ret.res.requestUrls[0].replace("http://zktuistatic.oss-cn-shenzhen.aliyuncs.com/", "http://zktfile.wxslzf.com/");
            console.log(pathname);
            if (pathname == "posterCoverUrl") {
                // 海报原稿
                vm.bPosterInfo.cover = reqUrl;
                document.getElementById('imgCoverUrl').src = vm.bPosterInfo.cover;
            }

            if (pathname == "posterBaseUrl") {
                // 海报底图
                vm.bPosterInfo.base = reqUrl;
                document.getElementById('imgBaseUrl').src = vm.bPosterInfo.base;
            }
        }).catch(function (err) {
            console.log(err);
        });
    }

    function getFileExtendingName(filename) {
        // 文件扩展名匹配正则
        var reg = /\.[^\.]+$/;
        var matches = reg.exec(filename);
        if (matches) {
            return matches[0];
        }
        return '';
    }
</script>
</body>
</html>
